//package com.xhsj.user.flowable.service;
//
//import com.google.common.collect.Lists;
//import com.google.common.collect.Maps;
//import com.xhsj.user.flowable.factory.FlowableFactory;
//import com.xhsj.user.flowable.vo.FlowableCallableVo;
//import com.xhsj.user.flowable.vo.FlowableTask;
//import com.xhsj.user.security.entity.LoginUser;
//import com.xhsj.user.utils.Message;
//import com.xhsj.user.utils.UserUtils;
//import org.flowable.bpmn.model.*;
//import org.flowable.engine.*;
//import org.flowable.engine.history.HistoricActivityInstance;
//import org.flowable.engine.history.HistoricProcessInstance;
//import org.flowable.engine.runtime.Execution;
//import org.flowable.engine.runtime.ProcessInstance;
//import org.flowable.identitylink.api.IdentityLink;
//import org.flowable.image.ProcessDiagramGenerator;
//import org.flowable.task.api.Task;
//import org.flowable.task.api.history.HistoricTaskInstance;
//import org.flowable.task.api.history.HistoricTaskInstanceQuery;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RequestParam;
//
//import javax.servlet.http.HttpServletResponse;
//import java.io.InputStream;
//import java.util.*;
//import java.util.stream.Collectors;
//
///**
// * @Description FlowableService
// * @Author suxiaolin
// * @Date 2021/1/4 13:16
// */
//@Service
//public class FlowableService {
//
//    @Autowired
//    private RuntimeService runtimeService;
//    @Autowired
//    private TaskService taskService;
//    @Autowired
//    private RepositoryService repositoryService;
//    @Autowired
//    private ProcessEngine processEngine;
//
//    @Autowired
//    private HistoryService historyService;
//
//
//
//    /**
//     * 启动流程
//     * @author 苏小林
//     * @date 2020/12/31 16:50
//     * @return: com.xhsj.user.utils.Message
//     */
//    public void startProcess(String businessKey, Map<String, Object> map, String processId, FlowableFactory<?> flowableFactory) throws Exception {
//        //启动流程
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processId,businessKey,map);
//
//        // 业务表添加流程实例id
//        FlowableCallableVo flowableCallableVo = new FlowableCallableVo();
//        flowableCallableVo.setProcessInstanceId(processInstance.getProcessInstanceId());
//        flowableFactory.callBack(flowableCallableVo);
//    }
//
//
//    /**
//     * 查询所有待审核任务
//     * @author 苏小林
//     * @date 2021/1/4 13:57
//     * @param: userId
//     * @param: processId
//     * @return: java.util.List<java.lang.String>
//     */
//    public List<FlowableTask> todoList(String userId,String processId) {
//        LoginUser loginAppUser = UserUtils.getLoginAppUser();
//        Long userId1 = loginAppUser.getUserId();
//        Set<String> sysRoles = loginAppUser.getSysRoles();
//        String collect = sysRoles.stream().collect(Collectors.joining(","));
//        // 查询当前用户任务
//        List<Task> tasks = taskService.createTaskQuery().processDefinitionKey(processId).taskAssignee(String.valueOf(userId1)).orderByTaskCreateTime().desc().list();
//
//        // 根据角色任务
//        List<Task> unsignedTasks = taskService.createTaskQuery().processDefinitionKey(processId).taskCandidateGroup(collect).orderByTaskCreateTime().desc().list();
//        tasks.addAll(unsignedTasks);
//        List<FlowableTask> businessKeys = Lists.newArrayList();
//        for (Task task : tasks) {
//            FlowableTask flowableTask = new FlowableTask();
//            String processInstanceId = task.getProcessInstanceId();
//            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).active().singleResult();
//            String businessKey = processInstance.getBusinessKey();
//            flowableTask.setBusinessKey(businessKey);
//            flowableTask.setProcessTaskId(task.getId());
//            flowableTask.setNodeName(task.getName());
//            flowableTask.setTaskDefinitionKey(task.getTaskDefinitionKey());
//            businessKeys.add(flowableTask);
//        }
//        return businessKeys;
//    }
//
//    public List<FlowableTask> hisTask(String userId,String processId) {
//        LoginUser loginAppUser = UserUtils.getLoginAppUser();
//        Long userId1 = loginAppUser.getUserId();
//        Set<String> sysRoles = loginAppUser.getSysRoles();
//        String collect = sysRoles.stream().collect(Collectors.joining(","));
//
//        List<HistoricTaskInstance> tasks = historyService.createHistoricTaskInstanceQuery().processDefinitionKey(processId).taskAssignee(String.valueOf(userId1)).orderByTaskCreateTime().desc().list();
//
//        List<HistoricTaskInstance> hiTasks = historyService.createHistoricTaskInstanceQuery().processDefinitionKey(processId).taskCandidateGroup(collect).orderByTaskCreateTime().desc().list();
//        tasks.addAll(hiTasks);
//
//        List<FlowableTask> businessKeys = Lists.newArrayList();
//        for (HistoricTaskInstance task : tasks) {
//
//            // 历史流程实例
//            HistoricProcessInstance hpi = historyService.createHistoricProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
//            String businessKey = hpi.getBusinessKey();
//            // 获取需要的历史数据
//
//            FlowableTask flowableTask = new FlowableTask();
//            flowableTask.setBusinessKey(businessKey);
//            flowableTask.setProcessTaskId(task.getId());
//            flowableTask.setNodeName(task.getName());
//            flowableTask.setTaskDefinitionKey(task.getTaskDefinitionKey());
//            businessKeys.add(flowableTask);
//        }
//
//        return businessKeys;
//    }
//
//
//    @Transactional(rollbackFor = Exception.class)
//    public void handleAudit(String taskId,Map<String, Object> map,FlowableFactory<?> flowableFactory) throws Exception {
//        FlowableCallableVo flowableCallableVo = new FlowableCallableVo();
//        Task currTask = taskService.createTaskQuery().taskId(taskId).singleResult();
//        if (currTask == null) {
//            throw new RuntimeException("流程不存在");
//        }
//
//        taskService.addComment(taskId,currTask.getProcessInstanceId(),String.valueOf(map.get("opinionText")));
//        taskService.complete(taskId, map);
//
//        HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery().taskId(taskId).singleResult();
//        Task task=taskService.createTaskQuery().processInstanceId(historicTaskInstance.getProcessInstanceId()).active().singleResult();
//
//        if (task != null) {
//            List<IdentityLink> identityLinksForTask = taskService.getIdentityLinksForTask(task.getId());
//            for (IdentityLink identityLink : identityLinksForTask) {
//                if ("candidate".equals(identityLink.getType())) {
//                    String groupId = identityLink.getGroupId();
//                    flowableCallableVo.getRoleKeyNames().add(groupId);
//                }
//            }
//        }
//
//        flowableFactory.callBack(flowableCallableVo);
//    }
//
//
//
//
//    /**
//     * 任务历史
//     *
//     * @param processId 部署id
//     */
//    public List<HistoricActivityInstance> getHistoryProcess(String processId) {
//        List<HistoricActivityInstance> list = historyService // 历史相关Service
//                .createHistoricActivityInstanceQuery() // 创建历史活动实例查询
//                .processInstanceId(processId) // 执行流程实例id
//                .finished()
//                .list();
//        return list;
//    }
//}
